Week 2 Answers (0909)

1 Import Data

  1. Import the package tidyverse, which contains several useful functions for data wrangling and visualization.

  2. Create a folder on your computer to store the dataset downloaded from NTU COOL or OpenIntro website.
    Set your working directory as that folder and import the datasets county.csv and loan50.csv.

library(tidyverse)
setwd("/your/working/directory")
county <- read.csv("county.csv")
loan <- read.csv("loan50.csv")

2 Scatter Plot

  1. Use county.csv to draw the scatter plot of unemployment_rate (x-axis) and per_capita_income (y-axis).
Tip

Hint: You don’t need to specify any parameter in geom_point().

ggplot(county, aes(x = unemployment_rate, y = per_capita_income)) + 
  geom_point()
  1. Draw the same graph again, but this time make the following adjustments:

    • adjust the point color to lightcoral
    • adjust the point size to 0.5
ggplot(county, aes(x = unemployment_rate, y = per_capita_income)) + 
  geom_point(color = "lightcoral", size = 0.5)

3 Histogram

Use loan.csv data to draw the histogram of loan_amount (set as x-axis), and adjust the parameter binwidth in geom_histogram() to 5000.

ggplot(loan, aes(x = loan_amount)) +
  geom_histogram(binwidth = 5000)